package com.china08.yxyapi.api.manage.sch.platform.website.image;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.mg.sws.SwsAlbums;
import com.china08.yxyapi.entity.mg.sws.SwsAlbumsImage;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.SwsAlbumsImageRepository;
import com.china08.yxyapi.repository.mg.YxySwsAlbumsRepository;

@CrossOrigin
@RestController("SwsAlbumsDetailController")
public class SwsAlbumsDetailController {

    @Autowired
    YxySwsAlbumsRepository yxySwsAlbumsRepository;

    @Autowired
    SwsAlbumsImageRepository swsAlbumsImageRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/swsalbums/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String id = reqModel.id;
        Assert.isTrue(StringUtils.isNotBlank(id), "未获取到相册");

        // 获取相册
        SwsAlbums swsAlbums = yxySwsAlbumsRepository.findById(id);
        Assert.isTrue(null != swsAlbums, "未获取到相册");

        // 相册id
        String albumId = swsAlbums.getId();

        // 相册图片
        List<SwsAlbumsImage> swsAlbumsImages = swsAlbumsImageRepository.findByAlbumId(albumId);
        Assert.isTrue(null != swsAlbumsImages, "相册图片为空");

        RespModel respModel = new RespModel();
        respModel.swsAlbums = swsAlbums;
        respModel.swsAlbumsImages = swsAlbumsImages;
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String id;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 相册
        public SwsAlbums swsAlbums;
        // 相册图片
        public List<SwsAlbumsImage> swsAlbumsImages;
    }
}
